/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.controller;

import com.alibaba.fastjson.JSON;
import com.lvyh.lightframe.job.admin.annotation.PermitLimit;
import com.lvyh.lightframe.job.admin.bean.JobAlarmConfigBean;
import com.lvyh.lightframe.job.admin.bean.JobTaskDefBean;
import com.lvyh.lightframe.job.admin.model.JobAlarmConfigParam;
import com.lvyh.lightframe.job.admin.model.JobAlarmUserParam;
import com.lvyh.lightframe.job.admin.processor.JobManageProcessor;
import com.lvyh.lightframe.job.common.Result;
import com.lvyh.lightframe.job.enums.ErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author lvyh 2021/04/11.
 */
@Controller
@RequestMapping("/alarm")
public class AlarmController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(AlarmController.class.getName());

    @Autowired
    private JobManageProcessor jobManageProcessor;

    @RequestMapping
    @PermitLimit(superAdmin = true)
    public String index(Model model, String taskName, String taskType) throws Exception {
        JobTaskDefBean jobTaskDefBean = jobManageProcessor.getJobTaskDefInfo(Integer.parseInt(taskType), taskName);
        if (jobTaskDefBean == null) {
            return "alarm/index";
        }
        JobAlarmConfigBean alarmConfigBean = jobManageProcessor.getJobAlarmConfigInfo(jobTaskDefBean.getId());
        if (jobTaskDefBean != null) {
            long smsInterval = Long.parseLong(alarmConfigBean.getSmsIntervalTime()) / (60 * 1000); // 分钟
            long emailInterval = Long.parseLong(alarmConfigBean.getEmailIntervalTime()) / (60 * 1000);

            alarmConfigBean.setEmailIntervalTime(String.valueOf(emailInterval));
            alarmConfigBean.setSmsIntervalTime(String.valueOf(smsInterval));
            model.addAttribute("alarmConfigBean", alarmConfigBean);
        }

        return "alarm/index";
    }

    @RequestMapping("/update")
    @PermitLimit(superAdmin = true)
    @ResponseBody
    public Result<String> update(JobAlarmConfigParam param) throws Exception {
        if (StringUtils.isEmpty(param.getAlarmName())) {
            return Result.create(ErrorCode.FAILED, "Alarm name must not be empty.");
        }

        JobAlarmConfigBean alarmConfigBean = jobManageProcessor.getJobAlarmConfigInfo(param.getObjectId());
        if (alarmConfigBean != null) {
            long smsInterval = Long.parseLong(param.getSmsIntervalTime()) * 60 * 1000; // 毫秒
            long emailInterval = Long.parseLong(param.getEmailIntervalTime()) * 60 * 1000;

            alarmConfigBean.setEmailIntervalTime(String.valueOf(emailInterval));
            alarmConfigBean.setSmsIntervalTime(String.valueOf(smsInterval));
            alarmConfigBean.setDescription(param.getDescription());
            alarmConfigBean.setAlarmType(param.getAlarmType());
            alarmConfigBean.setAlarmName(param.getAlarmName());
            alarmConfigBean.setStatus(param.getStatus());
            JobTaskDefBean jobTaskDefBean = jobManageProcessor.getJobTaskDefById(alarmConfigBean.getObjectId());
            if (jobTaskDefBean != null) {
                JobAlarmUserParam jobAlarmUserParam = new JobAlarmUserParam();
                jobAlarmUserParam.setUserName(jobTaskDefBean.getOwner());
                jobAlarmUserParam.setEmail(jobTaskDefBean.getOwnerEmail());
                jobAlarmUserParam.setPhone(jobTaskDefBean.getOwnerPhone());
                String userContact = JSON.toJSONString(jobAlarmUserParam);
                alarmConfigBean.setUserContact(userContact);
            }

            boolean result = jobManageProcessor.updateJobAlarmConfigBean(alarmConfigBean);
            return (result) ? Result.SUCCESS() : Result.FAILED();
        }

        return Result.FAILED();
    }


}
